package Utilities;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author callum
 */
public class ArrayBuilder {
    byte[] arr;
    int size;
    int index;

    public ArrayBuilder() {
        size = 10000;
        index = 0;
        arr = new byte[size];
    }

    public void append(byte[] a) {
        try {
            System.arraycopy(a, 0, arr, index, a.length);
            index += a.length;
        } catch (IndexOutOfBoundsException e) {
            size = size * 2;
            byte[] temp = new byte[size];
            System.arraycopy(arr, 0, temp, 0, arr.length);
            arr = temp;
            append(a);
        }
    }

    public void append(byte[] a, int length) {
        try {
            System.arraycopy(a, 0, arr, index, length);
            index += length;
        } catch (IndexOutOfBoundsException e) {
            size = size * 2;
            byte[] temp = new byte[size];
            System.arraycopy(arr, 0, temp, 0, arr.length);
            arr = temp;
            append(a, length);
        }
    }

    public void append(byte[] a, int offset, int length) {
        try {
            System.arraycopy(a, offset, arr, index, length);
            index += length;
        } catch (IndexOutOfBoundsException e) {
            size = size * 2;
            byte[] temp = new byte[size];
            System.arraycopy(arr, 0, temp, 0, arr.length);
            arr = temp;
            append(a, offset, length);
        }
    }

    public String getLine(int line) {
        //13 10
        int count = 0;
        int start = 0;
        int end = 0;
        while ((count < line) && end + 1 < index) {
            if (arr[end] == 13 && arr[end + 1] == 10) {
                count++;
            }
            end++;
        }
        if (end < index) {
            start = end;
            while ((count == line) && end + 1 < index) {
                if (arr[end] == 13 && arr[end + 1] == 10) {
                    count++;
                }
                end++;
            }
            return new String(arr, start, end - start);
        } else {
            return "";
        }
    }

    public int endHeader() {
        int line = 0;
        int end = 0;
        while (end + 3 < index) {
            if (arr[end] == 13 && arr[end + 1] == 10) {
                line++;
                end++;
                end++;
                if (arr[end] == 13 && arr[end + 1] == 10) {
                    return line;
                }
            }
            end++;
        }
        return line;
    }

    public int dataStart() {
        int end = 0;
        while (end + 3 < index) {
            if (arr[end] == 13 && arr[end + 1] == 10) {
                end++;
                end++;
                if (arr[end] == 13 && arr[end + 1] == 10) {
                    return end + 2;
                }
            }
            end++;
        }
        return -1;
    }

    public byte[] getArr() {
        return arr;
    }

    public int getSize() {
        return index;
    }
}